package com.kaisa.activity.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.kaisa.activity.BaseApp;
import com.kaisa.activity.R;
import com.kaisa.activity.adapter.HotSellAdapter;
import com.kaisa.activity.adapter.StartRoadAdapter;
import com.kaisa.activity.bean.DaysSell;
import com.kaisa.activity.bean.StartRoad;
import com.kaisa.activity.constant.UrlHttp;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

public class StartRoadActivity extends AppCompatActivity implements View.OnClickListener {
    @Bind(R.id.start_back)
    ImageView startBack;
    @Bind(R.id.start_title)
    TextView startTitle;
    @Bind(R.id.start_share)
    ImageView startShare;
    @Bind(R.id.start_lv)
    ListView startLv;
    private StartRoadAdapter adapter;
    private List<StartRoad.ThemeEntity> list = new ArrayList<StartRoad.ThemeEntity>();
    private View layout;
    private String title;
    private String url;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_start_road);
        ButterKnife.bind(this);
        Intent intent = getIntent();
        title = intent.getStringExtra("title");
        url = intent.getStringExtra("url");
        startTitle.setText(title);
        initView();
        initData();
    }

    private void initData() {
        startBack.setOnClickListener(this);
        startShare.setOnClickListener(this);
        x.http().get(new RequestParams(UrlHttp.START_ROAD_URL), new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String s) {
                StartRoad entity = JSON.parseObject(s, StartRoad.class);
                list.addAll(entity.getTheme());
                adapter.notifyDataSetChanged();
            }

            @Override
            public void onError(Throwable throwable, boolean b) {

            }

            @Override
            public void onCancelled(CancelledException e) {

            }

            @Override
            public void onFinished() {

            }
        });
        startLv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                //跳转到详情界面
                Intent intent= new Intent(StartRoadActivity.this,HomeProduceActivity.class);
                Bundle bundle = new Bundle();
                bundle.putSerializable("bean",list.get(position+1));
                intent.putExtras(bundle);
                startActivity(intent);
            }
        });
    }

    private void initView() {
        adapter = new StartRoadAdapter(list, this);
        layout = LayoutInflater.from(this).inflate(R.layout.start_top_item, null);
        startLv.addHeaderView(layout);
        startLv.setAdapter(adapter);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.start_back:
                this.finish();
                break;
            case R.id.start_share:
                BaseApp.getInstance().showShare(title,url);
                break;
        }
    }
}
